$(document).ready(function() {
    var cName = $("#commodity-name");
    var cPrice = $("#commodity-price");
    var cDescription = $("#commodity-description");
  

    var cNameTip = $("#commodity-nameTip");
    var cPriceTip = $("#commodity-priceTip");
    var cDescriptionTip = $("#commodity-priceTip");





   
    cName.keyup(function() {
        checkValidateCName(cName, cNameTip);
    });
    cPrice.keyup(function() {
        checkValidateCPrice(cPrice, cPriceTip);
    });
    cDescription.keyup(function() {
        checkValidateCDescription(cDescription, cDescriptionTip);
        updateLocation();
    });
    


    function checkValidateCName() {
        if (!validate(cName.val(), "\\w{3,50}$")) {
            cNameTip.html("Short name!");
            cName.css("border-color", "#ef3124");
            return false;
        } else {
            cName.css("border-color", "#79c30c");
            cNameTip.html("")
            return true;

        }
    }
    
        function checkValidateCPrice() {
        if (!validate(cName.val(), "^[0-9,.$")) {
            cPriceTip.html("Enter right price!");
            cPrice.css("border-color", "#ef3124");
            return false;
        } else {
            cPrice.css("border-color", "#79c30c");
            cPriceTip.html("")
            return true;

        }
    }

    function checkValidateCDescription() {
        if (!validate(cName.val(), "\\w{3,}$")) {
            cDescriptionTip.html("Short description.");
            cDescription.css("border-color", "#ef3124");
            return false;
        } else {
            cDescription.css("border-color", "#79c30c");
            cDescriptionTip.html("")
            return true;

        }
    }



    $("#create-btn").unbind('click').click(function(event) {
        
        $(".table-view .error").each(function() {
            $(this).html("");
        });
        $(":input").each(function() {
            $(this).css("border-color", "#CCC");
        });
        
        checkValidateCName(cName, cNameTip);
        checkValidateCPrice(cPrice, cPriceTip);
        checkValidateCDescription(cDescription, cDescriptionTip);

        
//        if (appartment.val() != "") {
//            checkValidateHome(appartment, appartmentTip);
//        }

        if (checkValidateCName(cName, cNameTip)
                && checkValidateCPrice(cPrice, cPriceTip)
                && checkValidateCName(cDescription, cDescriptionTip)) {
            sendData();
        }
    });

    function sendData() {
        var dataString = 'cName=' + cName.val() +
                '&cPrice=' + cPrice.val() +
                '&cDescription=' + cDescription.val();
        $.ajax({
            type: "POST",
            url: "Productcreate",
            data: dataString,
            success: function(data) {
                cNameTip.html(data.cName);
                cPriceTip.html(data.cPrice);
                cDescriptionTip.html(data.cDescription);
                if (typeof data.success !== "undefined")
                    showDialog();
            }
        });
    }

    function validate(value, pattern) {
        var reg = new RegExp(pattern);
        return reg.test(value);
    }
    
    function showDialog() {
        var dialog = '<div id="dark-font"></div>'
                       + '<div id="dialog-outer">' 
                       + '<div id="dialog">' 
                       + '<h1>Product is created.</h1>' 
                       + 'Now product is available in store.' 
                       + '<div style="text-align: center; margin-top: 30px;">' 
                       + '<input type="button" value="Warehouse page" id="warehouse-page"/>'
                       + '<input type="button" value="Create more" id="create-product"/>'
                       + '</div></div></div>';
        $("#content").after(dialog);
        $("#warehouse-page").click(function() {
            document.location.href = "admin/warehouse/warehouse.jsp";
        });
        $("#create-product").click(function() {
            document.location.href = "admin/warehouse/productcreate.jsp";
        });
    }
    
    
    
});

function startUploadTop(){
   document.getElementById('upload_process').style.visibility = 'visible';
   document.getElementById('upload_process').style.display = 'block';
   document.getElementById('upload_form').style.visibility = 'hidden';
   document.getElementById('upload_form').style.display = 'none';
   if(document.getElementById('upload_form').childNodes.item(0).innerHTML != "") {
    document.getElementById('upload_form').removeChild(document.getElementById('upload_form').childNodes.item(0));
   }
   return true;
}

function stopUpload(success,html){
   window.success=success;
   var result = '';
   if (success == 1){
   result = '<div id="infoText" style="top:49px;left:243px;color:red;position:relative;height:0;opacity:1;width:140px;text-align:center;">Файл загружен успешно!<\/div>';
   }
   else if (success == 0){
   result = '<div id="infoText" style="top:49px;left:243px;color:red;position:relative;height:0;opacity:1;width:140px;text-align:center;">Ошибка загрузки!<\/div>';
   }
   else if (success == -1){
    result = '<div id="infoText" style="top:49px;left:243px;color:red;position:relative;height:0;opacity:1;width:140px;text-align:center;">Ошибка!<\/div>'
   }
  
   document.getElementById('upload_process').style.visibility = 'hidden';
   document.getElementById('upload_process').style.display = 'none';
   var oldval = document.getElementById('upload_form').innerHTML;
   document.getElementById('upload_form').innerHTML = result + html + oldval; //
   document.getElementById('upload_form').style.display = 'block';
   document.getElementById('upload_form').style.visibility = 'visible';

   return true;   
}